class LogFormat {
    getClientIp(req) {
        return req.headers['x-forwarded-for'] ||
            req.connection.remoteAddress ||
            req.socket.remoteAddress ||
            req.connection.socket.remoteAddress || '';
    };

    formatReqLog(req) {
        let {originalUrl, method} = req;
        let params = JSON.stringify(method === "GET" ? req.query : req.body);
        let ip = this.getClientIp(req);//客户端ip地址
        let browserType = req.headers['user-agent'];
        return {originalUrl, method, ip, params, browserType}
    }

    formatErrorLog(err, req, res) {
        let {method, originalUrl} = req;
        let {status, name, message, stack} = err;
        stack = status === 500 ? stack : "";//只保留显示500错误的信息，其他信息的忽略stack
        return {status, name, message, stack, method, originalUrl};
    }
}

module.exports = new LogFormat();
